package com.wei.czz.framework.store.controller;

import com.wei.czz.common.dto.common.NameValue;
import com.wei.czz.common.dto.store.assign.*;
import com.wei.czz.common.jsr303.group.Save;
import com.wei.czz.common.jsr303.group.Search;
import com.wei.czz.common.jsr303.group.Submit;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.vo.store.assign.AssignFormVo;
import com.wei.czz.common.vo.store.assign.AssignVo;
import com.wei.czz.framework.store.manager.AssignManager;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-12-01 11:37:24
 * className: AssignStoreController
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("/assign-store")
@AllArgsConstructor
public class AssignStoreController {

    private static final Logger log = LoggerFactory.getLogger(AssignStoreController.class);

    private final AssignManager assignManager;

    /**
     * 分页获取已分单商店接口
     * @param assignFormVo 参数对象
     * @return 结果
     */
    @PostMapping("/page/list")
    public Result<PageDto<AssignListDto>> getMyAssignStorePageList(
            @Validated(Search.class) @RequestBody AssignFormVo assignFormVo) {
        log.info("开始分页获取已分单商店业务。{}", assignFormVo);

        PageDto<AssignListDto> pageDto = assignManager.getMyAssignStorePageList(assignFormVo);

        log.info("分页获取已分单商店业务结束");
        return Result.success(pageDto);
    }

    /**
     * 分页获取可以分单商店接口
     * @param assignFormVo 参数对象
     * @return 结果
     */
    @PostMapping("/assignable/page/list")
    public Result<PageDto<AssignManageListDto>> getAssignableStorePageList(
            @Validated(Search.class) @RequestBody AssignFormVo assignFormVo) {
        log.info("开始分页获取可以分单商店业务。{}", assignFormVo);

        PageDto<AssignManageListDto> pageDto = assignManager.getAssignableStorePageList(assignFormVo);

        log.info("分页获取可以分单商店业务结束");
        return Result.success(pageDto);
    }

    /**
     * 分页获取未分单商店接口
     * @param assignFormVo 参数对象
     * @return 结果
     */
    @PostMapping("/not-assign/page/list")
    public Result<PageDto<AssignListDto>> getNotAssignStorePageList(
            @Validated(Search.class) @RequestBody AssignFormVo assignFormVo) {
        log.info("开始分页获取未分单商店业务。{}", assignFormVo);

        PageDto<AssignListDto> pageDto = assignManager.getNotAssignStorePageList(assignFormVo);

        log.info("分页获取未分单商店业务结束");
        return Result.success(pageDto);
    }

    /**
     * 获取未分单商店所属用户接口
     * @return 结果
     */
    @GetMapping("/not-assign/user/list")
    public Result<List<NameValue<String>>> getNotAssignStoreUserList() {
        log.info("开始获取未分单商店所属用户业务");

        List<NameValue<String>> userList = assignManager.getNotAssignStoreUserList();

        log.info("获取未分单商店所属用户业务");
        return Result.success(userList);
    }

    /**
     * 获取商店分单记录接口
     * @param storeId  商店主键
     * @param masterId 主表主键
     * @return 结果
     */
    @GetMapping("/flow/list")
    public Result<List<AssignFlowListDto>> getAssignStoreFlowList(
            @RequestParam(value = "storeId", required = false) Long storeId,
            @RequestParam(value = "masterId", required = false) Long masterId) {
        log.info("开始获取商店分单记录业务。storeId={} businessId={}", storeId, masterId);
        if (ObjectUtils.allNull(storeId, masterId)) {
            log.info("商店主键和业务主键同时为空");
            return Result.clientFail("商店主键和主表主键不能同时为空");
        }
        List<AssignFlowListDto> assignFlowListDtoList = assignManager.getAssignStoreFlowList(storeId, masterId);

        log.info("获取商店分单记录业务结束");
        return Result.success(assignFlowListDtoList);
    }

    /**
     * 获取商店审核管理页枚举接口
     */
    @GetMapping("/audit/enums")
    public Result<AssignPageEnumDto> getMyAssignEnum() {
        log.info("开始获取商店审核管理页枚举业务");

        AssignPageEnumDto assignPageEnumDto = assignManager.getMyAssignEnum();

        log.info("获取商店审核管理页枚举业务结束");
        return Result.success(assignPageEnumDto);
    }

    /**
     * 获取商店分单管理页枚举接口
     * @return 结果
     */
    @GetMapping("/manage/enums")
    public Result<AssignManageEnumDto> getAssignStoreManageEnum() {
        log.info("开始获取商店分单管理页枚举业务");

        AssignManageEnumDto assignManageEnumDto = assignManager.getAssignStoreManageEnum();

        log.info("获取商店分单管理页枚举业务结束");
        return Result.success(assignManageEnumDto);
    }

    /**
     * 商店审核认领接口
     * @param assignVo 参数对象
     * @return 结果
     */
    @PostMapping("/claim")
    public Result<String> claimStore(@Validated(Save.class) @RequestBody AssignVo assignVo) {
        log.info("开始商店审核认领业务。{}", assignVo);

        String tipMessage = assignManager.claimStore(assignVo);

        log.info("商店审核认领业务结束");
        return Result.success(tipMessage);
    }

    /**
     * 商店分单接口
     * @param assignVo 参数对象
     * @return 结果
     */
    @PostMapping("/assign")
    public Result<String> assignStore(@Validated(Submit.class) @RequestBody AssignVo assignVo) {
        log.info("开始商店分单业务。{}", assignVo);

        String tipMessage = assignManager.assignStore(assignVo);

        log.info("商店分单业务结束");
        return Result.success(tipMessage);
    }

}
